package com.yu.servlet;

import com.yu.Pojo.User;
import com.yu.service.UserService;
import com.yu.serviceImpl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //指定像页面输出编码
        resp.setContentType("text/html;charset=UTF-8");
        //接收用户输入的信息
        String userCode = req.getParameter("userCode");
        String userName = req.getParameter("userName");
        String gender = req.getParameter("gender");
        Integer genderChange = new Integer(gender);
        String phone = req.getParameter("phone");
        //把信息 存到User中
        User user = new User(null,userCode,userName,genderChange,phone);
        //此处可以直接调用UserDao 来插入数据，但是由于三层架构 先访问service。 servlert 只调用 service
        UserService userService = new UserServiceImpl();
        try {
            int i = userService.addUser(user);
            if(i>0){
                //resp.setContentType("text/html;charset=UTF-8");
                String rs = "success!";
                PrintWriter out = resp.getWriter();
                out.print(rs);
                System.out.println("前端访问插入成功！！！");
                //req.getRequestDispatcher("/index.jsp").forward(req,resp);
            }else {
                //resp.setContentType("text/html;charset=UTF-8");
                System.out.println("重复名字，前端访问插入失败1！！！,");
                String rs = "名字重复，失败!";
                PrintWriter out = resp.getWriter();
                out.print(rs);
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("111，前端访问插入失败！！！");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {



    }

}
